var scanQRCode = angular.module("scanQRCode", ["scService"]);
scanQRCode.controller("scanQRCodeController",function($scope,$rootScope, $window, scCodeService, $location){
	//获取是否充电
	var openCharge = localStorage.getItem("openCharge");
	// alert(openCharge);
	if(openCharge == "true"){
		// alert("进入充电了吗？")
		window.location.href = "http://" + window.location.host + "/appserver/static/wechat-ht/index.html#/charging"	
	}else{
			//获取微信签名
	scCodeService.getJsSign().then(function(data){
		/*var datastr=JSON.stringify(data);
		alert(datastr);*/
		if(data.code="200"){
			var wxparams=data.data
			
			//通过config接口注入权限验证配置
			wx.config({
		      debug: false,
		      appId: wxparams.appId,
		      timestamp: wxparams.timeStamp,
		      nonceStr: wxparams.nonceStr,
		      signature: wxparams.sign,
		      jsApiList: [
		        'checkJsApi',
		        'onMenuShareTimeline',
		        'onMenuShareAppMessage',
		        'onMenuShareQQ',
		        'onMenuShareWeibo',
		        'onMenuShareQZone',
		        'hideMenuItems',
		        'showMenuItems',
		        'hideAllNonBaseMenuItem',
		        'showAllNonBaseMenuItem',
		        'translateVoice',
		        'startRecord',
		        'stopRecord',
		        'onVoiceRecordEnd',
		        'playVoice',
		        'onVoicePlayEnd',
		        'pauseVoice',
		        'stopVoice',
		        'uploadVoice',
		        'downloadVoice',
		        'chooseImage',
		        'previewImage',
		        'uploadImage',
		        'downloadImage',
		        'getNetworkType',
		        'openLocation',
		        'getLocation',
		        'hideOptionMenu',
		        'showOptionMenu',
		        'closeWindow',
		        'scanQRCode',
		        'chooseWXPay',
		        'openProductSpecificView',
		        'addCard',
		        'chooseCard',
		        'openCard'
		      ]
		  });
			// var num=0;
			// var qrCodeTime = setInterval(function(){
			// 	num++;
			// 	if(num > 15){
			// 	alert("+++");
			// 	clearInterval(qrCodeTime);
			// 	window.location.href = "http://weixin.mawansan.com/static/wechat-ht/index.html#/manualCode";	
			// 	}
			// },1000);

			wx.ready(function () {
			  // 9.1.2 扫描二维码并返回结果
				wx.scanQRCode({
				  needResult: 1,
				  desc: 'scanQRCode desc',
				  success: function (res) {
					    	var result = JSON.stringify(res.resultStr); // 当needResult 为 1 时，扫码返回的结果
					    	var rLen = result.length - 1;
					    	var linkCode = result.slice(1,17);
					    	// alert(linkCode);
//					    	if(linkCode == "http://wx.haote.online/"){
//					    		var num = result.indexOf("online/") + 3;
					    	if(linkCode == "http://id.dz.tt/"){
					    		var num = result.indexOf("tt/") + 3;
					    		var qrCode = result.slice(num,rLen);
						    	var postData = {
						    		qrCode:qrCode
						    	}
						    	// alert(qrCode);
						    	scCodeService.getChargerDetailByQrCode(postData).then(function(data){
						    		var result = JSON.stringify(data); 
						    		// alert(result);
						    		if(data.code == "200"){
						    			// alert(JSON.stringify(data.data));
						    			//alert("扫码"+JSON.stringify($scope.chargerDetail));
						    			$scope.chargerDetail = data.data;
						    			localStorage.setItem("chargerDetail", JSON.stringify($scope.chargerDetail));
						    			window.location.href = "http://weixin.mawansan.com/static/wechat-ht/index.html#/chargeStarted";
						    		}else{
						    			window.location.href = "http://weixin.mawansan.com/static/wechat-ht/index.html#/manualCode";	    			
						    		}
						    	})
					    	}else{
					    			window.location.href = "http://weixin.mawansan.com/static/wechat-ht/index.html#/errorScan";
					    	}
						}
				});
			});

			wx.error(function (res) {
				// alert(res.errMsg);
			});

		}
		
	});
	}
	
})